package com.aimashi.railwaytoolmanagesystem.utils;

import java.util.List;

import android.content.Context;

import com.aimashi.commonlib.util.StringUtil;
import com.aimashi.railwaytoolmanagesystem.R;
import com.aimashi.railwaytoolmanagesystem.bean.Card;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.util.LogUtils;

/**
 * 
 * @author zhaoys
 * 
 */
public class DatabaseCardUtil {
	// xutils
	private DbUtils db;

	public DatabaseCardUtil(Context context) {
		db = DbUtils.create(context);
	}

	public void initRecord() {
		int[] musicSource = {R.raw.dianzidaochi,R.raw.jixiedaochi,R.raw.qidaoqi1,R.raw.qidaoqi2,R.raw.qidaoqi3,R.raw.pingbanche,R.raw.qiangguangdeng,R.raw.zcd300neiranbang1,R.raw.zcd300neiranbang2,R.raw.zcd300neiranbang3,R.raw.zcd300neiranbang4,R.raw.zcd300neiranbang5,R.raw.zcd300neiranbang6,R.raw.zcd300neiranbang7,R.raw.zcd300neiranbang8,R.raw.zcd300neiranbang9,R.raw.zcd300neiranbang10,R.raw.gsmrshouji,R.raw.motorolagp3688,R.raw.youtong,R.raw.sizhuapa1,R.raw.sizhuapa2,R.raw.sizhuapa3,R.raw.sizhuapa4,R.raw.sizhuapa5,R.raw.sizhuapa6,R.raw.yaobang1,R.raw.yaobang2};
		//R.raw.dianzidaochi,测试标签,00112233445566778899AACC,
		String[] tempSource = "广南一数显式电子道尺0级,000000000000000000010003,广南一机械道尺-1,000000000000000000010004,广南一液压起道器（有砟）-1,000000000000000000010013,广南一液压起道器（有砟）-2,000000000000000000010014,广南一液压起道器（有砟）-3,000000000000000000010015,广南一平板车-1,000000000000000000010024,广南一轻便式多功能强光灯（JIW5281/LD),000000000000000000010034,广南一ZCD300内燃直捣棒-1,300833B2DDD9014000020043,广南一ZCD300内燃直捣棒-2,300833B2DDD9014000020044,广南一ZCD300内燃直捣棒-3,300833B2DDD9014000020045,广南一ZCD300内燃直捣棒-4,300833B2DDD9014000020046,广南一ZCD300内燃直捣棒-5,300833B2DDD9014000020047,广南一ZCD300内燃直捣棒-6,300833B2DDD9014000020048,广南一ZCD300内燃直捣棒-7,300833B2DDD9014000020049,广南一ZCD300内燃直捣棒-8,300833B2DDD9014000020050,广南一ZCD300内燃直捣棒-9,300833B2DDD9014000020051,广南一ZCD300内燃直捣棒-10,300833B2DDD9014000020052,广南一GSM-R手机-1,300833B2DDD9014000020062,广南一对讲机MOTOROLAGP3688-1,300833B2DDD9014000020065,广南一15升油桶-1,100100000000201501160325,广南一四爪扒-1,000000000000000000000038,广南一四爪扒-2,000000000000000000000039,广南一四爪扒-3,000000000000000000000040,广南一四爪扒-4,000000000000000000000041,广南一四爪扒-5,000000000000000000000042,广南一四爪扒-6,000000000000000000000044,广南一摇棒-1,300833B2DDD9014000040050,广南一摇棒-2,300833B2DDD9014000040051".split(",");
		int j = 0;
		for(int i=0;i < tempSource.length-1;){
			LogUtils.e("index: "+i+ "--j:index" + j+ "\n");
			Card model = new Card();
			model.setEpcDesc(tempSource[i]);
			model.setEpcMusic(musicSource[j]);
			j++;
			model.setEpcNumber(tempSource[i+1]);
			LogUtils.e("EpcDesc: "+model.getEpcDesc() +" EpcNumber: "+model.getEpcNumber() + "\n");
			add(model);
			i = i + 2;
		}
	}

	/**
	 * 向数据库中添加数�?
	 * 
	 * @param Card
	 * @throws DbException
	 */
	public void add(Card Card) {
		try {
			db.save(Card);
		} catch (DbException e) {
			e.printStackTrace();
			LogUtils.i("写卡出错");
		}
	}

	/**
	 * 通过Id查找对应的Card
	 * 
	 * @param id
	 *            唯一标识Id
	 * @return
	 * @throws DbException
	 */
	public Card findCardById(String epcNumber) {
		Card entity = null;
		try {
			entity = db.findById(Card.class, epcNumber); // db.findFirst(Selector.from(Parent.class).where("name","=","test"));
		} catch (DbException e) {
			LogUtils.i("查找保存epc标签出错");
			e.printStackTrace();
		}
		return entity;
	}

	/**
	 * 查找Card
	 * 
	 * @return
	 * @throws DbException
	 */
	public List<Card> findAllCardByName(String searchValue) {
		List<Card> entity = null;
		try {
			if (!StringUtil.isNull(searchValue)) {
				entity = db.findAll(Selector.from(Card.class).orderBy(
						"epcNumber", true));
			} else {
				entity = db.findAll(Selector.from(Card.class)
						.where("epcDesc", "like", "%" + searchValue + "%")
						.orderBy("epcNumber", true));
			}
		} catch (DbException e) {
			LogUtils.i("查找标签出错");
			e.printStackTrace();
		}
		return entity;
	}

	/**
	 * 通过EpcNumber删除对应数据
	 * 
	 * @param id
	 * @throws DbException
	 */
	public void deleteCardByEpcNumber(String epcNumber) {
		try {
			db.deleteById(Card.class, epcNumber);
		} catch (DbException e) {
			LogUtils.i("删除标签出错");
			e.printStackTrace();
		}
	}

	public void deleteCardAll() {
		try {
			db.deleteAll(Card.class);
			db.dropTable(Card.class);
		} catch (DbException e) {
			LogUtils.i("删除所有标签出错");
			e.printStackTrace();
		}
	}

	/**
	 * 更新标签信息
	 * 
	 * @param entity
	 *            实体
	 * @throws DbException
	 */
	public void UpdateCard(Card entity) {
		deleteCardByEpcNumber(entity.getEpcNumber());
		add(entity);
	}

}
